package com.diyphonegadgets.freeblog.data.parser;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.diyphonegadgets.freeblog.utils.Debug;


public class BasicParser {

	public static String getString(JSONObject jsonObject, String key) {
		String value;
		try {
			value = jsonObject.getString(key);
		} catch (JSONException e) {
			Debug.debugWarning("JSON", "the key " + key
					+ " dosn't exist in this json object");
			return "";
		}
		return value;
	}

	public static int getInt(JSONObject jsonObject, String key) {
		int value;
		try {
			value = jsonObject.getInt(key);
		} catch (JSONException e) {
			Debug.debugWarning("JSON", "the key " + key
					+ " dosn't exist in this json object");
			return 0;
		}
		return value;
	}

	public static boolean getBoolean(JSONObject jsonObject, String key) {
		boolean value;
		try {
			value = jsonObject.getBoolean(key);
		} catch (JSONException e) {
			Debug.debugWarning("JSON", "the key " + key
					+ " dosn't exist in this json object");
			return false;
		}
		return value;
	}

	public static double getDouble(JSONObject jsonObject, String key) {
		double value;
		try {
			value = jsonObject.getDouble(key);
		} catch (JSONException e) {
			Debug.debugWarning("JSON", "the key " + key
					+ " doesn't exist in this json object");
			return 0.0;
		}
		return value;
	}

	public static JSONArray getJSONArray(JSONObject jsonObject, String key) {
		JSONArray array;
		try {
			array = jsonObject.getJSONArray(key);
		} catch (JSONException e) {
			Debug.debugWarning("JSON", "the key " + key
					+ " doesn't exist or it's not an array in this json object");
			return null;
		}
		return array;
	}

	protected static String getBlock(String json, int openBracketIndex,
			char openingChar, char closingChar) {
		String text = "";
		boolean isBlockFound = false;
		int nextOpenBracketIndex = json.indexOf(openingChar,
				openBracketIndex + 1);
		int nextCloseBracketIndex = json.indexOf(closingChar,
				openBracketIndex + 1);
		while (!isBlockFound) {
			if (nextCloseBracketIndex == -1)// Problem
			{
				return null;
			}
			if (nextOpenBracketIndex == -1 && nextCloseBracketIndex != -1)// only
																			// one
																			// block
																			// and
																			// no
																			// inside
																			// block
			{
				text = json.substring(openBracketIndex + 1,
						nextCloseBracketIndex);
				isBlockFound = true;
			}
			if (nextOpenBracketIndex > nextCloseBracketIndex)// we find the
																// first block
																// that we
																// wanted
			{
				text = json.substring(openBracketIndex + 1,
						nextCloseBracketIndex);
				isBlockFound = true;
			}
			if (nextOpenBracketIndex < nextCloseBracketIndex)// there is an
																// inside block
			{
				nextOpenBracketIndex = json.indexOf(openingChar,
						nextCloseBracketIndex + 1);
				nextCloseBracketIndex = json.indexOf(closingChar,
						nextCloseBracketIndex + 1);
			}
		}
		return text;
	}
}
